﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using System.Web.Security;
using System.Security.Principal;
using Vmago.Models.DBML;

namespace Vmago.Controllers
{
    public class ConsumerController : Controller
    {
        public ActionResult Index()
        {
            // Add action logic here
            throw new NotImplementedException();
        }

        public ActionResult entry()
        {
            return View();
        }

        public ActionResult entryVIP()
        {

            return View();
        }

        public ActionResult UpdateVIP()
        {
            var User = new MembershipDataContext();
            var RolesInfo = from u in User.aspnet_Users
                            from r in User.aspnet_Roles
                            from uir in User.aspnet_UsersInRoles
                            where u.UserId == (Guid)Membership.GetUser().ProviderUserKey
                            where u.UserId == uir.UserId
                            where uir.RoleId == r.RoleId
                            select r;

            switch (RolesInfo.First().RoleName)
            {
                case "consumerNormal":
                    Roles.RemoveUserFromRole(Membership.GetUser().UserName.ToString(), "consumerNormal");
                    Roles.AddUserToRole(Membership.GetUser().UserName.ToString(), "consumerVIP"); break;
                case "coporateNormal":
                    Roles.RemoveUserFromRole(Membership.GetUser().UserName.ToString(), "coporateNormal");
                    Roles.AddUserToRole(Membership.GetUser().UserName.ToString(), "coporateVIP"); break;
                default:
                    break;
            }

            return RedirectToAction("Index", "Home");
        }

        public ActionResult RemoveVIP()
        {
            var User = new MembershipDataContext();
            var RolesInfo = from u in User.aspnet_Users
                            from r in User.aspnet_Roles
                            from uir in User.aspnet_UsersInRoles
                            where u.UserId == (Guid)Membership.GetUser().ProviderUserKey
                            where u.UserId == uir.UserId
                            where uir.RoleId == r.RoleId
                            select r;
            switch (RolesInfo.First().RoleName)
            {
                case "consumerVIP":
                    Roles.RemoveUserFromRole(Membership.GetUser().UserName.ToString(), "consumerVIP");
                    Roles.AddUserToRole(Membership.GetUser().UserName.ToString(), "consumerNormal"); break;
                case "coporateVIP":
                    Roles.RemoveUserFromRole(Membership.GetUser().UserName.ToString(), "coporateVIP");
                    Roles.AddUserToRole(Membership.GetUser().UserName.ToString(), "coporateNormal"); break;
                default:
                    break;
            }
            return RedirectToAction("Index", "Home");
        }

    }
}
